<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
  import loginApi from 'api/login'
  import Cargo from '@/components/cargo'
  import User from '@/components/cargo/user'
  import Password from '@/components/cargo/password'
  import {routerMap} from '@/router'
  import {mapMutations,mapGetters} from 'vuex'
  export default {
    name: 'App',
    created() {
      const keys = window.localStorage.getItem('keys')
      if (!keys) {
        this.$router.push('login')
      }else {
        this.addRoutes(JSON.parse(keys))
      }
    },
    computed: {
      ...mapGetters(['keys'])
    },
    watch: {
      keys(newKeys) {
        if(newKeys.length) {
          this.addRoutes(newKeys)
        }
      }
    },
    methods:{
      addRoutes(keys) {
        let res = [{
          path: '/',
          name: 'Cargo',
          component: Cargo,
          children: []
        }]
        keys.forEach(key => {
          res[0].children.push(routerMap[key])
        })
        this.$router.addRoutes(res)
        this.setRoutes(res)
      },
      ...mapMutations({
        setRoutes: 'SET_ROUTES'
      })
    }
  }
</script>

<style>

</style>
